package com.ants.boot.wx.controller;

import com.ants.boot.core.Response;
import com.ants.boot.core.security.utils.SecurityUtils;
import com.ants.boot.wx.dto.wxaddress.AddAddressDto;
import com.ants.boot.wx.dto.wxaddress.DelAddressDto;
import com.ants.boot.wx.dto.wxaddress.DetailDto;
import com.ants.boot.wx.dto.wxaddress.EditAddressDto;
import com.ants.boot.wx.service.WxAddressService;
import com.ants.boot.wx.vo.wxaddress.WxAddressVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 蚂蚁会花呗
 * @date 2022/6/1 14:45
 */
@RestController
@RequestMapping("wx/address")
@Api(tags = "收货地址")
public class WxAddressController {

    @Autowired
    private WxAddressService wxAddressService;



    @GetMapping("list_by_user_id")
    @ApiOperation(value = "当前用户的收件列表",tags = "用户的收件列表")
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功！",response = WxAddressVo.class)
    })
    public Response listByUserId(){
        Integer userId = SecurityUtils.getLoginUser().getUserId();
        return Response.okData(wxAddressService.listByUserId(userId));
    }

    @PostMapping("detail")
    @ApiOperation(value = "地址详情",tags = "根据主键id获取详情数据")
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功！",response = WxAddressVo.class)
    })
    public Response detail(@RequestBody @Validated DetailDto dto){
        return Response.okData(wxAddressService.getById(dto.getId()));
    }

    @PostMapping("add_address")
    @ApiOperation(value = "新增地址",tags = "新增收件人地址")
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功！")
    })
    public Response addAddress(@RequestBody @Validated AddAddressDto dto){
        boolean result = wxAddressService.addAddress(dto);
        if (!result){
            return Response.fail();
        }
        return Response.ok();
    }

    @PostMapping("edit_address")
    @ApiOperation(value = "更新地址",tags = "更新收件人地址")
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功！")
    })
    public Response editAddress(@RequestBody @Validated EditAddressDto dto){
        boolean result = wxAddressService.editAddress(dto);
        if (!result){
            return Response.fail();
        }
        return Response.ok();
    }

    /**
     * 删除意见建议
     * @return
     */
    @PostMapping("del_address")
    @ApiOperation(value = "删除地址",notes = "删除收件人地址")
    @ApiResponses({
            @ApiResponse(code = 200,message = "操作成功！")
    })
    public Response delAddress(@RequestBody @Validated DelAddressDto dto){
        boolean result = wxAddressService.delAddress(dto.getIds());
        if (!result){
            return Response.fail();
        }
        return Response.ok();
    }

}
